<?php

class MobileBasketController extends MobileController {

    static $model_name = 'basket';
    static $display_name = '我的餐盘';
	/*
	|--------------------------------------------------------------------------
	| Default Home Controller
	|--------------------------------------------------------------------------
	|
	| You may wish to use controllers instead of, or in addition to, Closure
	| based routes. That's great! Here is an example controller method to
	| get you started. To route to this controller, just add the route:
	|
	|	Route::get('/', 'HomeController@showWelcome');
	|
	*/


    /**
     * 获取我的餐盘
     */
    public function getIndex(){

        $title = '我的餐盘';
        $input['member_id'] = $this->member->id;
        $result =  $this->getService()->getList($input);
        $entries = $result['entries'];
        $links = $result['links'];

        return View::make(self::$view_path.'.basket.list', compact('entries', 'links', 'title'));
    }
    /**
     * 添加food 到餐盘
     */
    public function addFood(){
        $foodId = Input::get('id');
        $count = Input::get('count');

        $result =  $this->getService()->addFood($this->member->id, $foodId, $count);
        if(!$result){
            return Redirect::action('MobileBasketController@getIndex')->with('errors',$this->getService()->errors());
        }
        return Redirect::action('MobileBasketController@getIndex')->with('success','已添加至购物车');
    }
    /**
     * 添加food 到餐盘 JSON
     */
    public function addFoodForJSON(){
        $foodId = Input::get('id');
        $count = Input::get('count');

        $result =  $this->getService()->addFood($this->member->id, $foodId, $count);
        if(!$result){
            return json_encode(array('result'=>false));
        }
        return json_encode(array('result'=>true));
    }

    /**
     * 从餐盘中移除food
     * @param $food
     */
    public function dropFood($food){
        $result =  $this->getService()->dropFood($this->member->id, $food->id);
        if(!$result){
            return Redirect::action('MobileBasketController@getIndex')->with('errors',$this->getService()->errors());
        }
        return Redirect::action('MobileBasketController@getIndex')->with('success','已从购物车中移除该餐品');
    }

    /**
     * 从餐盘中移除food JSON
     */
    public function dropFoodForJSON(){
        $foodId = Input::get('id');

        $result =  $this->getService()->dropFood($this->member->id, $foodId);
        if(!$result){
            return json_encode(array('result'=>false));
        }
        return json_encode(array('result'=>true));
    }


    public function updateFoodCount(){
        $foodId = Input::get('food_id');
        $count = Input::get('count');

        $result =  $this->getService()->addFood($this->member->id, $foodId, $count);
        if(!$result){
            json_encode(array('result'=>false, 'msg'=>$this->getService()->errors()->getMessages()));
        }
        return json_encode(array('result'=>true));

    }


    public function getService() {
        if(is_null($this->service)){
            $this->service = new MobileBasketService();
        }
        return $this->service;
    }

}
